#include <iostream>
#include "AVLmap.h"
#include "RBTree.h"
#include "RBTTimer.h"
#include <memory>
#include <thread>

void text1(){
    AVLMap<int,std::string> mymap;
    mymap.put(1,"aa");
    mymap.put(3,"cc");
    mymap.put(2,"bb");
    mymap.put(5,"ee");
    mymap.put(6,"ff");
    mymap.put(4,"dd");
    std::vector<std::pair<int,std::string>> iter = mymap.inorder();
    for(auto& i : iter){
        std::cout<< "key:" << i.first << " value:"<<i.second<<std::endl;
    }
    std::string* val = mymap.get(4);
    if (val)
        std::cout << "4: " << *val << std::endl;
    else
        std::cout << "not exist" << std::endl;
    mymap.remove(4);

    RBTree<int,std::string> mymap2;

    mymap2.insert(1,"aa");
    mymap2.insert(3,"cc");
    mymap2.insert(2,"bb");
    mymap2.insert(5,"ee");
    mymap2.insert(6,"ff");
    mymap2.insert(4,"dd");
    mymap2.inorderTraversal();
    mymap2.remove(5);
    mymap2.inorderTraversal();
}

struct character{
    int32_t level;
    std::string name;
};

void printHello(void* ctx) {
    std::cout << "Hello! Context: " << (const char*)ctx << std::endl;
}

void printTime(void*) {
    auto now = std::chrono::system_clock::now();
    std::time_t t = std::chrono::system_clock::to_time_t(now);
    std::cout << "Current time: " << std::ctime(&t);
}
int main() {
    text1();
    Timer timer;
    struct character ch;
    ch.level = 1;
    ch.name = "aaa";
    timer.addtask(20000, printHello, (void*)"20s");
    timer.addtask(15000, printHello, (void*)"15s");
    timer.addtask(5000, printHello, (void*)"5s");
    timer.addtask(10000, printHello, (void*)"10s");
    // 添加重复任务（每秒触发一次）
    timer.addtask(1000, printTime, nullptr, true, 1000);

    while (true) {
        int64_t nextExpire = timer.tick();
        if (nextExpire == -1) break;

        auto now = Timer::getCurrentTime();
        if (nextExpire > now) {
            std::this_thread::sleep_for(std::chrono::milliseconds(nextExpire - now));
        }
    }

    return 0;
}
